package com.jingan.nursinghome.regulatory.dialog2;

import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.model.Categirues2;
import com.jingan.nursinghome.regulatory.view.MyActionBar;
import com.jingan.nursinghome.regulatory.view.triangleView2;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;

/**
 * Created by 彭治铭 on 2017/10/25.
 */

public class XuantiMenuDialog extends BaseDialog {

    public XuantiMenuDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_richang2_xuanti, false, false);
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
        initUI();
        adapterUI();
    }

    public static XuantiMenuDialog xuantiDialog;

    public static XuantiMenuDialog getInstance(BaseActivity baseActivity) {
        if (isFinishing(xuantiDialog)) {
            xuantiDialog = new XuantiMenuDialog(baseActivity);
        }
        return xuantiDialog;
    }

    MyActionBar myActionBar;
    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;

    private void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "选题");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(baseActivity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
                super.getItemOffsets(outRect, itemPosition, parent);
                outRect.top = ProportionUtils.getInstance().adapterInt(25);
                outRect.left = outRect.top;
                outRect.right = outRect.left;
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<XuantiMenuDialog.MyAdapter.MyViewHoldView> {

        List<String> list;

        public MyAdapter(List<String> list) {
            this.list = list;
        }

        class MyViewHoldView extends RecyclerView.ViewHolder {
            public MyViewHoldView(View itemView) {
                super(itemView);
            }
        }

        @Override
        public MyViewHoldView onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHoldView(new triangleView2(parent.getContext()));
        }

        @Override
        public void onBindViewHolder(MyViewHoldView holder, final int position) {
            triangleView2 triangleView2 = (com.jingan.nursinghome.regulatory.view.triangleView2) holder.itemView;
            triangleView2.setIndexAndInfo(position + 1 + "", list.get(position));
            triangleView2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //XuantiMenuDialog2.getInstance(baseActivity, list.get(position));
                    String subject_f = MicrometerTable.getInstance().categories.get(position).getId();
                    Http.getInstance().getCategories2(subject_f, new BaseListenner<List<Categirues2>>() {
                        @Override
                        public void onResult(final List<Categirues2> result) {
                            baseActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    XuantiMenuDialog3Little.getInstance(baseActivity, list.get(position), result);
                                }
                            });
                        }
                    });
                }
            });
        }

        @Override
        public int getItemCount() {
            if (list != null) {
                return list.size();
            }
            return MicrometerTable.getInstance().categories.size();
        }
    }

    private void adapterUI() {
        ProportionUtils.getInstance().adapterView(findViewById(R.id.dialog_richang2_constraintLayout));
    }


    @Override
    protected void listener() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < MicrometerTable.getInstance().categories.size(); i++) {
//            Log.e("test", "类别:\t" + MicrometerTable.getInstance().categories);
//            Log.e("test", "个数:\t" + MicrometerTable.getInstance().categories.size());
//            Log.e("test", "单个:\t" + MicrometerTable.getInstance().categories.get(i));
//            Log.e("test", "名称:\t" + MicrometerTable.getInstance().categories.get(i).getName());
            if(MicrometerTable.getInstance().categories.get(i)!=null&&MicrometerTable.getInstance().categories.get(i).getName()!=null){
                list.add(MicrometerTable.getInstance().categories.get(i).getName());
            }
        }

//        list.add("设施环境标准");
//        list.add("照料服务标准");
//        list.add("管理标准");

        recyclerView.setAdapter(new MyAdapter(list));
    }

    @Override
    protected void recycleView() {

    }
}
